package cn.genmer.test.security.controller;

import cn.genmer.test.security.thread.MyCallable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

@RestController
@RequestMapping("/rest/test")
public class HelloController {

    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @Autowired
            private HelloController helloController;

    FutureTask task =null;

    @GetMapping("/hello")
    public String hello(@RequestParam String username, HttpServletRequest request){
//        helloController.tesd();
        String[] usernames = request.getParameterValues("username");
        String username1 = request.getParameter("username");
        return "hello security 运行成功!";
    }


    @GetMapping("/index")
    public String index(){
        return "hello index 运行成功!";
    }

    public void tesd(){
        System.out.println("内部调用");
    }

    @GetMapping("/async")
    public String async(){
        Callable callable = new MyCallable();
        task = new FutureTask(callable);
        new Thread(task,"子线程").start();
        return "数据正在下载!";
    }

    @GetMapping("/async/result")
    public String asyncResult(){
        String result = "数据正在生成中！";
        try {
            //获取子线程的返回值
          if (task.isDone()){
              result = "数据下载完成，请查看：" + task.get();
              System.out.println("获取成功");
          }
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

//    @Scheduled(cron = "*/5 * * * * ?")
//    @Scheduled(cron = "*/5 * * * * ?")
    public void onTime(){
        System.out.println(new Date());
    }
}
